import Vue from 'vue'
import Router from 'vue-router'
import '@/assets/css/global.css'

import Login from '@/components/Login'
import Home from '@/components/Home'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home
    }
  ]
})

// 挂载路由导航拦截
// to 将要访问的路径, from 从那个页面来, next 函数, 强制跳转路径
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    return next();
  }
  // 判断是否存在token
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) {
    // 跳转登录页
    return next("/login");
  }
  next();
});

export default router;

